home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / DEB / INPUT.C < prev    next >
C/C++ Source or Header  |  1994-12-12  |  5KB  |  234 lines

  1. /*
  2. ** FILE: input.c
  3. **
  4. ** DESC: doskey-like read() function
  5. **
  6. ** (C) Rainer Schnitker 1994
  7. **
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** as published by the Free Software Foundation; either version
  11. ** 2 of the License, or (at your option) any later version.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include <conio.h>
  19. #include <malloc.h>
  20.  
  21. /*
  22. ** the read function, arguments:
  23. ** input buffer, size of buffer, boolean (newline after <return> pressed)
  24. ** return size of input
  25. */
  26.  
  27. int get_input(char *, int, int);
  28.  
  29. /*
  30. ** this function reads a key from the console
  31. ** low byte: ascii
  32. ** high byte: scan code, if ascii = 0
  33. */
  34. static int _read_keybrd(void)
  35. {
  36. #ifdef __GO32__
  37.     int c = getkey();
  38.     if ((c >> 8 == 1) || (c >> 8 == 2))
  39.     c <<= 8;
  40. #else
  41.     int c = getch();
  42.     if (c == 0 || c == 0xE0)
  43.     c = getch() << 8;
  44. #endif
  45.     return c;
  46. }
  47.  
  48. /* history strings can contain 256 entries */
  49. static char * oldstr[256];
  50. static int istr = 0;
  51.  
  52. /* get previus string */
  53. static void cpy_up_string(char *str)
  54. {
  55.     if (istr == 0 || !str)
  56.     return;
  57.  
  58.     istr--;
  59.     if (istr == 0) {
  60.     if (oldstr[255])
  61.         istr = 255;
  62.     }
  63.     if (oldstr[istr])
  64.     strcpy(str, oldstr[istr]);
  65.     else
  66.     istr ++;
  67. }
  68.  
  69. static void cpy_down_string(char *str)
  70. {
  71.     if (istr == 0 || !str)
  72.     return;
  73.  
  74.     istr++;
  75.     if (istr == 256)
  76.     istr = 1;
  77.  
  78.     if (oldstr[istr])
  79.     strcpy(str, oldstr[istr]);
  80.     else
  81.     istr--;
  82. }
  83.  
  84. /* put next string */
  85. static void cpy_new_strcpy(char *str)
  86. {
  87.     static int line_index = 0;
  88.  
  89.     line_index++;
  90.     if (line_index >= 256)
  91.     line_index = 1;
  92.     if (! oldstr[line_index])
  93.     oldstr[line_index] = malloc(strlen(str)+1);
  94.     else
  95.     oldstr[line_index] = realloc(oldstr[line_index], strlen(str)+1);
  96.     if (oldstr[line_index])
  97.     strcpy(oldstr[line_index], str);
  98.     istr = line_index + 1;
  99. }
  100.  
  101. int get_input(char *str, int size, int opt_newline)
  102. {
  103.     char ins_flag;
  104.     int strpos;
  105.     int strend;
  106.  
  107.     unsigned int key, scan;
  108.     unsigned char ascii = 0;
  109.     int i;
  110.  
  111.     strpos = 0;
  112.     strend = 0;
  113.     ins_flag = 1;
  114.     memset(str, 0, size);
  115.  
  116.     for (;;) {
  117.     key = _read_keybrd();
  118.  
  119.     scan = (key >> 8) & 0xFF;
  120.     ascii = (unsigned char) (key & 0xFF);
  121.  
  122.     if (ascii == 0xE0)
  123.         ascii = 0;
  124.  
  125.     if (ascii >= 32) {    /* show char */
  126.         if (strpos == strend) {
  127.         putchar(ascii);
  128.         *(str + strpos) = ascii;
  129.         strend++;
  130.         } else if (ins_flag) {    /* shift right chars */
  131.         for (i = strend; i >= strpos; --i)
  132.             *(str + i + 1) = *(str + i);
  133.         *(str + strpos) = ascii;
  134.         printf("%s",str + strpos);
  135.         for (i = strend; i > strpos; i--)
  136.             putchar('\b');
  137.         strend++;
  138.         } else {
  139.         putchar(ascii);
  140.         *(str + strpos) = ascii;
  141.         }
  142.         strpos++;
  143.         fflush(stdout);
  144.         continue;
  145.     }
  146.     /* backspace or delete */
  147.     else if ((ascii == '\b' && strpos != 0)
  148.          || (scan == 0x53 && strend != strpos)) {
  149.         if (ascii == '\b') {
  150.         putchar('\b');
  151.         strpos--;
  152.         }
  153.         for (i = strpos; i < strend; ++i)
  154.         *(str + i) = *(str + i + 1);
  155.         strend--;
  156.         *(str + strend) = '\0';
  157.         printf("%s",str + strpos);
  158.         putchar(' ');
  159.         putchar('\b');
  160.         for (i = strend; i > strpos; i--)
  161.         putchar('\b');
  162.         fflush(stdout);
  163.         continue;
  164.     } else if (ascii == 13) {   /* return */
  165.         if (opt_newline)
  166.         putchar('\n');
  167.         while (strend > 0 && *(str + strend - 1) == ' ')
  168.         strend--;
  169.         *(str + strend) = 0;
  170.         if (strend>0)
  171.         cpy_new_strcpy(str);
  172.         break;
  173.     } else if (ascii == 27) {    /* ESC */
  174.         for (; strpos < strend; strpos++)
  175.         putchar(' ');
  176.         for (; strpos != 0; strpos--) {
  177.         putchar('\b');
  178.         putchar(' ');
  179.         putchar('\b');
  180.         }
  181.         memset(str, 0, size);
  182.         strpos = strend = 0;
  183.         fflush(stdout);
  184.         continue;
  185.     } else if (scan == 0x4b && strpos != 0) {
  186.         putchar('\b');
  187.         strpos--;
  188.         fflush(stdout);
  189.         continue;
  190.     } else if (scan == 0x48) {    /* up */
  191.         for (; strpos != 0; strpos--) {
  192.         putchar('\b');
  193.         putchar(' ');
  194.         putchar('\b');
  195.         }
  196.         cpy_up_string(str);
  197.         printf("%s",str);
  198.         strpos = strend = strlen(str);
  199.         fflush(stdout);
  200.         continue;
  201.     } else if (scan == 0x50) {    /* down */
  202.         for (; strpos != 0; strpos--) {
  203.         putchar('\b');
  204.         putchar(' ');
  205.         putchar('\b');
  206.         }
  207.         cpy_down_string(str);
  208.         printf("%s",str);
  209.         strpos = strend = strlen(str);
  210.         fflush(stdout);
  211.         continue;
  212.     } else if (scan == 0x4d && strpos < strend) {    /* -> */
  213.         putchar(*(str + strpos));
  214.         strpos++;
  215.         fflush(stdout);
  216.         continue;
  217.     } else if (scan == 0x47) {    /* home */
  218.         for (; strpos != 0; strpos--)
  219.         putchar('\b');
  220.         strpos = 0;
  221.         fflush(stdout);
  222.         continue;
  223.     } else if (scan == 0x4F) {    /* end */
  224.         printf("%s",str + strpos);
  225.         strpos = strend;
  226.         fflush(stdout);
  227.         continue;
  228.     } else if (scan == 0x52)/* ins */
  229.         ins_flag ^= 1;
  230.     fflush(stdout);
  231.     }
  232.     return strend;
  233. }
  234.